//避免变量冲突和污染
;
(function () {

    $(function () {
        $.idcode.setCode(); //加载生成验证码方法

        //定义自己的规则
        $.validator.addMethod("checkName", (value, element, param) => {
            var reg = /^[a-z]+/i;
            return reg.test(value);
        })

        $.validator.addMethod("checkPhone", (value, element, param) => {
            var reg = /^[1][3-9]\d{9}$/i;
            return reg.test(value);
        })
        $.validator.addMethod("checkEmail", (value, element, param) => {
            var reg = /^\w+(\.\w+)*@\w+\.\w+(\.\w+)*$/;
            return reg.test(value);
        })



        //给表单绑定事件
        $("form").validate({
            //规则
            rules: {
                uname: {
                    required: true, //必填
                    rangelength: [6, 16],
                    checkName: true
                },
                upwd: {
                    required: true, //必填
                    rangelength: [6, 16],
                },
                upwd2: {
                    equalTo: "#upwd"
                },
                uphone: {
                    checkPhone: true
                },
                uemail: {
                    checkEmail: true
                }
            },
            //提示
            messages: {
                uname: {
                    required: '用户名必填',
                    rangelength: "用户名必须是{0}-{1}",
                    checkName: "用户名不合法"
                },
                upwd: {
                    required: '密码必填',
                    rangelength: "密码必须是{0}-{1}"
                },
                upwd2: {
                    equalTo: "2次密码不一致"
                },
                uphone: {
                    checkPhone: "手机号不合法"
                },
                uemail: {
                    checkEmail: "邮箱不合法"
                }
            },
            //提交
            submitHandler: function () {

                var IsBy = $.idcode.validateCode();

                if (!IsBy) {

                    layer.msg('验证码错误!');
                    return false;
                } else {

                    //校验都通过,注册到cookie

                    var obj = {
                        uname: $('[name="uname"]').val(),
                        upwd: $('[name="upwd"]').val(),
                        usex: $('[name="usex"]:checked').val(),
                        uphone: $('[name="uphone"]').val(),
                        uemail: $('[name="uemail"]').val(),
                    }

                    //1.先读取cookie,转换成数组
                    var userList = JSON.parse($.cookie("users") || '[]');
                    //2.变量数组 判断是否已经注册
                    var flag = userList.some((el) => el.uname == obj.uname)
                    //3.如果没有注册,就保存到数组里
                    if (flag == true) {

                        layer.alert('用户名已注册!');
                        return false;
                    }
                    //4.把数组保存到cookie
                    userList.push(obj);
                    $.cookie.raw = true; //设置不使用编码
                    $.cookie("users", JSON.stringify(userList), {
                        expires: 10
                    });


                    //注册成功,是否理解去登录

                    layer.confirm("注册成功,是否立即去登录?", {
                        skin: 'layui-layer-molv',
                        type: 1,
                        btn: ["yes", "noyes"]
                    }, (index) => {
                        layer.close(index);
                        window.location = "login.html"


                    }, () => {})


                }

                return false;
            }
        })





    });
})();